package com.rockcent.job.task;

import com.rockcent.helper.SysMessageHelper;
import com.rockcent.mall.lib.constants.SysMessageConstants;
import com.rockcent.mall.lib.domain.*;
import com.rockcent.mall.lib.domain.enumclass.*;
import com.rockcent.mall.lib.dto.EvaluationDto;
import com.rockcent.mall.lib.helper.OrderEventHelper;
import com.rockcent.mall.lib.repository.*;
import com.rockcent.service.MallCashCouponSrv;
import com.rockcent.service.OrderService;
import com.rockcent.service.ProductSrv;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangjh on 2016/11/24.
 */
@Component
public class OrderTask {

    final Logger log = LoggerFactory.getLogger(OrderTask.class);


    @Autowired
    private MerchantOrderRepository merchantOrderRepository;

    @Autowired
    private MarketOrderRepository marketOrderRepository;

    @Autowired
    private MerchantOrderItemRepository merchantOrderItemRepository;

    @Autowired
    private ProductQuantityRepository productQuantityRepository;


    @Autowired
    private MallOrderRepository mallOrderRepository;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderEventHelper orderEventHelper;

    @Autowired
    private MallCashCouponSrv mallCashCouponSrv;

    @Autowired
    private ProductSrv productSrv;

    /**
     * 15天，到期自动确认收货（C端需要提示用户），订单状态已完成，标记为待评价
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void changeOrderStatusToFinish() {
//        log.info("======================>15天，到期自动确认收货（C端需要提示用户），订单状态已完成，标记为待评价 begin>>>");
        List<MerchantOrder> orders = merchantOrderRepository.queryAllPayedOrder();


        if (orders != null) {

            orders.forEach(order -> {
                //更改mall_order状态
                MallOrder mallOrder = mallOrderRepository.findOne(order.getMallOrderId());
                if (null != mallOrder) {
                    mallOrder.setStatus(MallOrderStatus.FINISHED);
                    mallOrderRepository.save(mallOrder);
                }


                //更改merchant_order状态
                MerchantOrder merchantOrder = merchantOrderRepository.findOne(order.getId());

                MarketOrder marketOrder = marketOrderRepository.findTop1ByOrderNum(order.getMarketOrderNum());


                if (null != merchantOrder) {
                    merchantOrder.setStatus(OrderStatus.FINISHED);
                    merchantOrder.setEvaluateStatus(EvaluateStatus.WAIT_EVALUATE);
                    merchantOrderRepository.save(merchantOrder);
                }

                if (null != marketOrder) {
                    marketOrder.setStatus(OrderStatus.FINISHED);
                    marketOrderRepository.save(marketOrder);
                }


            });


        }

//        log.info("======================>15天，到期自动确认收货（C端需要提示用户），订单状态已完成，标记为待评价 end<<<");

    }

    /**
     * 15天，到期自动好评
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void automaticEvaluation() {
//        log.info("======================>15天，到期自动好评 begin>>>");
        OrderStatus orderStatus = OrderStatus.FINISHED;
        EvaluateStatus evaluateStatus = EvaluateStatus.WAIT_EVALUATE;
        List<MerchantOrder> orders = merchantOrderRepository.findByStatusAndEvaluateStatus(orderStatus, evaluateStatus);


        if (orders != null) {

            orders.forEach(order -> {
                List<Evaluation> evaluationList = new ArrayList<>();
                EvaluationDto evaluationDto = new EvaluationDto();
                evaluationDto.setEvaluationList(evaluationList);

                List<MerchantOrderItem> items = merchantOrderItemRepository.findByMerchantOrderId(order.getId());
                items.forEach(item -> {
                    Evaluation evaluation = new Evaluation();
                    evaluation.setComment("好评");
//                    evaluation.setCustomerId(item.getCustomerId());
                    evaluation.setCommentCustomerName("匿名");
                    evaluation.setMerchantId(item.getMerchantId());
                    evaluation.setProductId(item.getProductId());
                    evaluation.setProductName(item.getProductName());
                    evaluation.setRangeStar(5);
                    evaluation.setMerchantOrderNum(item.getMerchantOrderNum());
                    evaluation.setHideName(true);
                    evaluationList.add(evaluation);

                });

                productSrv.addEv(null, evaluationDto, order);
            });


        }

//        log.info("======================>15天，到期自动好评 end<<<");

    }

    /**
     * 15天，到期不提自动执行取消订单流程，资金原路返还，现金券返还账户，过期不可再提货（C端需要提示用户）
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void cancelOrderOfWaitShopPickpup() {
//        log.info("======================>15天，到期不提自动执行取消订单流程，资金原路返还，现金券返还账户，过期不可再提货（C端需要提示用户）begin>>>");
        cancel(OrderStatus.WAIT_SHOP_PICKUP);
//        log.info("======================>15天，到期不提自动执行取消订单流程，资金原路返还，现金券返还账户，过期不可再提货（C端需要提示用户）end<<<");

    }


    /**
     * 7天不发货，自动取消订单，资金原路返还消费者，订单状态变成已取消
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void cancelOrderOfWaitShipped() {
//        log.info("======================>7天不发货，自动取消订单，资金原路返还消费者，现金券返还账户，订单状态变成已取消 begin>>>");
        cancel(OrderStatus.WAIT_SHIPPED);
//        log.info("======================>7天不发货，自动取消订单，资金原路返还消费者，现金券返还账户，订单状态变成已取消 end<<<");
    }

    /**
     * 7天不备货，自动取消订单，资金原路返还消费者，订单状态变成已取消
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void cancelOrderOfWaitStock() {
//        log.info("======================>7天不备货，自动取消订单，资金原路返还消费者，现金券返还账户，订单状态变为已取消begin>>>");
        cancel(OrderStatus.WAIT_STOCK);
//        log.info("======================>7天不备货，自动取消订单，资金原路返还消费者，现金券返还账户，订单状态变为已取消end>>>");
    }

    /**
     * 支付有效期为24小时（即是订单有效期），时间过后，订单状态自动变成已取消，现金券恢复未使用状态
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void cancelOrderOfUnpayedExpired() {

//        log.info("======================>支付有效期为24小时（即是订单有效期），时间过后，订单状态自动变成已取消，现金券恢复未使用状态 begin>>>");
        List<MallOrder> mallOrders = mallOrderRepository.queryAllExpiredOrder();

        mallOrders.forEach(mallOrder -> {

            try {
                Long mallOrderId = mallOrder.getId();
                //---查询所有子订单（肯定部位空）
                List<MerchantOrder> merchantOrders = merchantOrderRepository.findByMallOrderId(mallOrderId);

                merchantOrders.forEach(order -> {

                    //更改mall_order状态
                    updateOrderStatus(order);
                    //更新库存
                    updateOrderItemQuantity(order);
                    String orderNum = order.getOrderNum();
                    String content = SysMessageConstants.SYS_CANCELORDER_OF_UNPAYEDEXPIRED.getContent();
                    content = MessageFormat.format(content, orderNum);

                    String frontedUrl = SysMessageConstants.SYS_CANCELORDER_OF_UNPAYEDEXPIRED.getFrontendUrl();
                    frontedUrl = MessageFormat.format(frontedUrl, orderNum);

                    //广播消息
                    try {
                        SysMessage message = SysMessageHelper.SINGLETON.createMsgBean(SysMessageConstants.SYS_CANCELORDER_OF_UNPAYEDEXPIRED, SysMessageType.OP, content);
                        message.setFrontendUrl(frontedUrl);
                        //广播消息
                        SysMessageHelper.SINGLETON.pushMsgByMallAndMerchantId(message, null, order.getMerchantId());

                    } catch (Exception e) {
                        log.error("cancelOrderOfUnpayedExpired sendMessage fail:", e);
                    }

                });

                MallOrder mallOrder1 = mallOrderRepository.findOne(mallOrderId);
                mallOrder1.setStatus(MallOrderStatus.CANCEL);
                mallOrderRepository.save(mallOrder1);

                refundIntegrationCoupon(mallOrder);

            } catch (Exception e) {
                log.error("cancelOrderOfUnpayedExpired task fail:", e);
                throw e;
            }


        });
//        log.info("======================>支付有效期为24小时（即是订单有效期），时间过后，订单状态自动变成已取消，现金券恢复未使用状态 end>>>");
    }


//    /**
//     * 单个服务产品：服务到期未使用，自动执行取消订单流程，资金原路返还，现金券返还账户，不可再使用，订单状态为已取消
//     * 多个服务产品：部分核销
//     */
//    @Scheduled(cron = "0 0/1 * * * ?")
//    @Transactional
//    public void cancelOrderOfServiceProductExpired() {
//
//        log.info("======================>可退服务产品到期未使用，自动退款begin>>>");
//        cancel(OrderStatus.WAIT_USE);
//        log.info("======================>可退服务产品到期未使用，自动退款end>>>");
//
//    }


    /**
     * 单个服务产品：服务到期未使用，自动执行取消订单流程，资金原路返还，现金券返还账户，不可再使用，订单状态为已取消
     * 多个服务产品：部分核销
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional
    public void cancelOrderOfServiceProductExpired() {

        log.info("======================>服务到期未使用  begin>>>");
        List<MerchantOrderItem> canCancelMerchantOrders = merchantOrderItemRepository.queryAllCanCancelMerchantOrders("YES");
        log.info("======================>过期的服务产品订单数有：" + canCancelMerchantOrders.size());

        //---遍历取消可取消子订单开始
        //更改merchant_order状态
        if (canCancelMerchantOrders != null && canCancelMerchantOrders.size() > 0) {
//            log.info("======================>过期的服务产品订单数有：{}", canCancelMerchantOrders.size());
            for (MerchantOrderItem merchantOrderItem : canCancelMerchantOrders) {
                try {
//                    log.info("======================>merchantOrderItem：{}", merchantOrderItem);
                    Long buyCount = merchantOrderItem.getBuyCount();
                    Long verifyCount = merchantOrderItem.getVerifyCount();
                    BigDecimal discountAmount = merchantOrderItem.getDiscountAmount();
                    Long merchantOrderId = merchantOrderItem.getMerchantOrderId();
//                    log.info("======================>verifyCount:{},buyCount:{}", verifyCount, buyCount);
                    MerchantOrder merchantOrder = merchantOrderRepository.findOne(merchantOrderId);

                    if (null != merchantOrder) {
                        Long mallOrderId = merchantOrder.getMallOrderId();
                        MallOrder mallOrder = mallOrderRepository.findOne(mallOrderId);
//                    MerchantOrderMerchantRole merchantRole = merchantOrder.getMerchantRole();
                        String mallOrderNum = mallOrder.getOrderNum();
                        String merchantOrderNum = merchantOrder.getOrderNum();
                        Long merchantOrderMallId = merchantOrder.getMallId();
                        Long customerId = merchantOrder.getCustomerId();
                        //退款金额
                        BigDecimal amount;
                        if (verifyCount > 0) {
                            amount = discountAmount.multiply( BigDecimal.valueOf(new Double(buyCount - verifyCount) / buyCount));
                            mallOrder.setStatus(MallOrderStatus.PART_FINISHED);
                        } else {
                            //返回实际金额
                            amount = mallOrder.getActualAmount();
                            mallOrder.setStatus(MallOrderStatus.FULL_REFUND);
                            //全部未核销，现金券回退
                            refundIntegrationCoupon(mallOrder);
                        }

//                        log.info("======================>退款金额{}", amount);

                        orderService.refund(customerId, mallOrderNum, merchantOrderNum, merchantOrderMallId, amount);

                        updateOrderStatus(merchantOrder);

                        mallOrderRepository.save(mallOrder);


                        //广播消息
                        try {
                            String content = SysMessageConstants.SYS_CANCELORDER_OF_SERVICEPRODUCT_EXPIRED.getContent();
                            content = MessageFormat.format(content, merchantOrderNum);

                            String frontedUrl = SysMessageConstants.SYS_CANCELORDER_OF_SERVICEPRODUCT_EXPIRED.getFrontendUrl();
                            frontedUrl = MessageFormat.format(frontedUrl, merchantOrderNum);

                            SysMessage message = SysMessageHelper.SINGLETON.createMsgBean(SysMessageConstants.SYS_CANCELORDER_OF_SERVICEPRODUCT_EXPIRED, SysMessageType.OP, content);
                            message.setFrontendUrl(frontedUrl);
                            //广播消息
                            SysMessageHelper.SINGLETON.pushMsgByMallAndMerchantId(message, null, merchantOrder.getMerchantId());

                        } catch (Exception e) {
                            log.error("cancelOrderOfUnpayedExpired sendMessage fail:", e);
                        }
                    }
                } catch (Exception e) {
                    log.error("======================>服务到期未使用 err:", e);
                    throw e;
                }

            }
        }
//        log.info("======================>服务到期未使用 end<<<");


    }

    private void updateOrderStatus(MerchantOrder order) {
        updateOrderStatus(order, OrderStatus.CANCEL);
    }

    /**
     * 取消订单：更改订单状态
     *
     * @param order
     */

    private void updateOrderStatus(MerchantOrder order, OrderStatus status) {
        //更改mall_order状态
//        MallOrder mallOrder = mallOrderRepository.findOne(order.getMallOrderId());

        //更改merchant_order状态
        MerchantOrder merchantOrderSupply = merchantOrderRepository.findOne(order.getId());

        MerchantOrder merchantOrderDistributor = null;
        String marketOrderNum = order.getMarketOrderNum();
        if (StringUtils.isNotBlank(marketOrderNum)) {
            merchantOrderDistributor = merchantOrderRepository.findTop1ByMarketOrderNumAndMerchantRole(marketOrderNum, MerchantOrderMerchantRole.DISTRIBUTOR);
        }


        MarketOrder marketOrder = marketOrderRepository.findTop1ByOrderNum(marketOrderNum);


        if (null != merchantOrderSupply) {
            merchantOrderSupply.setStatus(status);
            merchantOrderRepository.save(merchantOrderSupply);
        }
        if (null != merchantOrderDistributor) {
            merchantOrderDistributor.setStatus(status);
            merchantOrderRepository.save(merchantOrderDistributor);
        }

        if (null != marketOrder) {
            marketOrder.setStatus(status);
            marketOrderRepository.save(marketOrder);

        }


    }


    /**
     * 取消订单,流程
     *
     * @param status
     */
    private void cancel(OrderStatus status) {

        //查询门店订单所有可以提货过期可以取消的大订单id：唯一
        List<Long> mallOrderIds = queryAllMallOrderIdsExpired(status);
//        log.info("======================>mallOrderIds:{}", mallOrderIds);
        if (mallOrderIds != null && mallOrderIds.size() > 0) {

            //--遍历order开始
            mallOrderIds.forEach(mallOrderId -> {
                try {
                    //---所有可以取消的子订单：过滤了分销商的订单
                    List<MerchantOrder> canCancelMerchantOrders = getCancelMerchantOrders(mallOrderId, status);
//                    log.info("======================>canCancelMerchantOrders:{}", canCancelMerchantOrders);
                    if (canCancelMerchantOrders != null && canCancelMerchantOrders.size() > 0) {
                        MallOrder mallOrder = mallOrderRepository.findOne(mallOrderId);

                        //---查询所有子订单（肯定不为空）
                        List<MerchantOrder> merchantOrders = merchantOrderRepository.findByMallOrderId(mallOrderId);


                        boolean canRefundCanCoupon = false;

                        //单个产品的订单，现金券可退
                        int canCancelMerchantOrderSize = 0;
                        int allMerchantOrderSize = 0;
                        //可退金额
                        BigDecimal refundAmount =  BigDecimal.valueOf(0);
                        //是否单个产品下单
                        if (canCancelMerchantOrders != null) {
                            canCancelMerchantOrderSize = canCancelMerchantOrders.size();
                            allMerchantOrderSize = merchantOrders.size();
                            if (canCancelMerchantOrderSize == allMerchantOrderSize) {
                                canRefundCanCoupon = true;
                                mallOrder.setStatus(MallOrderStatus.FULL_REFUND);
                                refundAmount = mallOrder.getActualAmount();
                            } else {
                                mallOrder.setStatus(MallOrderStatus.PART_FINISHED);
                            }
                        }
//                        log.info("======================>mallOrder:{}", mallOrder);
//
//                        log.info("======================>canCancelMerchantOrderSize:{},allMerchantOrderSize:{}", canCancelMerchantOrderSize, allMerchantOrderSize);
//                        log.info("======================>canRefundCanCoupon:{}", canRefundCanCoupon);
                        Long customerId = mallOrder.getCustomerId();
                        String merchantOrderNum = mallOrder.getOrderNum();
                        Long mallId = mallOrder.getMallId();
                        String orderNum = mallOrder.getOrderNum();

                        //---遍历取消可取消子订单开始
                        //更改merchant_order状态
                        for (MerchantOrder merchantOrder : canCancelMerchantOrders) {
//                            log.info("======================>merchantOrder:{}", merchantOrder);
                            RefundType refundType = merchantOrder.getRefundType();
                            if (refundType == RefundType.REFUNDED) {
                                continue;
                            }
                            //更改mall_order状态
                            updateOrderStatus(merchantOrder);

                            orderEventHelper.saveOrderEvent(merchantOrder, OrderEventType.CANCEL, "系统主动取消订单", null);

                            //更新库存
                            updateOrderItemQuantity(merchantOrder);

                            //可退金额
                            refundAmount = merchantOrder.getDiscountAmount();
//                            customerId = merchantOrder.getCustomerId();
                            merchantOrderNum = merchantOrder.getOrderNum();
                            mallId = merchantOrder.getMallId();

                            if (!canRefundCanCoupon) {
                                //----子订单退款流程
                                orderService.refund(customerId, orderNum, merchantOrderNum, mallId, refundAmount);

                            }
                            SysMessageConstants sysMessageConstants = null;
                            String frontedUrl = null;
                            switch (status) {
                                case WAIT_SHOP_PICKUP: {
                                    sysMessageConstants = SysMessageConstants.SYS_CANCELORDER_OF_WAITSHOPPICKPUP;
                                    frontedUrl = SysMessageConstants.SYS_CANCELORDER_OF_WAITSHOPPICKPUP.getFrontendUrl();
                                    frontedUrl = MessageFormat.format(frontedUrl, merchantOrderNum);
                                    break;
                                }
                                case WAIT_SHIPPED: {
                                    sysMessageConstants = SysMessageConstants.SYS_CANCELORDER_OF_WAITSHIPPED;
                                    frontedUrl = SysMessageConstants.SYS_CANCELORDER_OF_WAITSHIPPED.getFrontendUrl();
                                    frontedUrl = MessageFormat.format(frontedUrl, merchantOrderNum);
                                    break;
                                }
                                case WAIT_STOCK: {//七天不备货
                                    sysMessageConstants = null;
                                    break;
                                }
                                case WAIT_USE: {
                                    sysMessageConstants = SysMessageConstants.SYS_CANCELORDER_OF_SERVICEPRODUCT_EXPIRED;
                                    frontedUrl = SysMessageConstants.SYS_CANCELORDER_OF_SERVICEPRODUCT_EXPIRED.getFrontendUrl();
                                    frontedUrl = MessageFormat.format(frontedUrl, merchantOrderNum);
                                    break;
                                }
                                default: {
                                    break;
                                }
                            }


                            String content = sysMessageConstants.getContent();
                            content = MessageFormat.format(content, merchantOrderNum);


                            //广播消息
                            try {
                                SysMessage message = SysMessageHelper.SINGLETON.createMsgBean(SysMessageConstants.SYS_GIFT_PACKAGE_EXPIRED, SysMessageType.OP, content);
                                message.setFrontendUrl(frontedUrl);
                                //广播消息
                                SysMessageHelper.SINGLETON.pushMsgByMallAndMerchantId(message, null, merchantOrder.getMerchantId());

                            } catch (Exception e) {
                                log.error("cancelOrderOfUnpayedExpired sendMessage fail:", e);
                            }
                        }


                        if (canRefundCanCoupon) {
                            orderService.refund(customerId, orderNum, merchantOrderNum, mallId, refundAmount);
                            refundIntegrationCoupon(mallOrder);
                        }

                        mallOrderRepository.save(mallOrder);


                    }


                    //--遍历order结束

                } catch (Exception e) {
//                    log.error("======================>取消订单出现异常 mallOrderId：{},status:{},err:{}", mallOrderId, status.getValue(), e);
                    throw e;
                }

            });
        }

    }


    /**
     * 退换现金券和积分卡券
     *
     * @param mallOrder
     */
    private BigDecimal refundIntegrationCoupon(MallOrder mallOrder) {
        return mallCashCouponSrv.refundIntegrationCoupon(mallOrder);
    }

    /**
     * 更新库存：实体产品，子订单肯定是全部核销
     *
     * @param order
     */
    private void updateOrderItemQuantity(MerchantOrder order) {
//        log.info("======================>order:" + order);
        //改变订单项对应产品库存
        List<MerchantOrderItem> orderItems = merchantOrderItemRepository.findByMerchantOrderIdAndMerchantId(order.getId(), order.getMerchantId());
        if (!orderItems.isEmpty()) {
            orderItems.forEach(merchantOrderItem -> {
//                log.info("======================>merchantOrderItem:" + merchantOrderItem);
                Long productId = merchantOrderItem.getProductId();
                Product product = productSrv.findOne(productId);
                if (null != product && null != product.getMarketId()) {
                    productId = product.getMarketId();
                } else {
                    log.error("======================>productId:{}的产品不存在", productId);
                    return;
                }
                Long merchantId = merchantOrderItem.getMerchantId();
                Long mallId = merchantOrderItem.getMallId();
//                Product product = productQuantityRepository.findTop1ByMallIdAndMerchantIdAndProductId(mallId,merchantId,productId);
                Long count = merchantOrderItem.getBuyCount();
                ProductQuantity productQuantity = productQuantityRepository.findTop1ByMallIdAndMerchantIdAndProductId(mallId, merchantId, productId);
                if (null != productQuantity) {
                    Long quantity = productQuantity.getQuantity();
                    productQuantity.setQuantity(quantity + count);
                    Long frozenQuantity = productQuantity.getFrozenQuantity();
                    productQuantity.setFrozenQuantity(frozenQuantity - count);
                    productQuantityRepository.save(productQuantity);
                } else {
                    log.error("======================>productId:{}的产品库存记录不存在", productId);
                }
            });
        }
    }


    /**
     * @param status
     * @return
     */
    private List<Long> queryAllMallOrderIdsExpired(OrderStatus status) {


        List<Long> mallOrderIds = new ArrayList<>();

        switch (status) {
            case WAIT_SHOP_PICKUP: {
                mallOrderIds = merchantOrderRepository.queryAllMallOrderIdsUnPickupAndExpired();
                break;
            }
            case WAIT_SHIPPED: {
                mallOrderIds = merchantOrderRepository.queryAllMallOrderIdsWaitShippedAndExpired();
                break;
            }
            case WAIT_STOCK: {
                mallOrderIds = merchantOrderRepository.queryAllMallOrderIdsWaitStockAndExpired();
                break;
            }
            case WAIT_USE: {
                mallOrderIds = merchantOrderRepository.queryAllMallOrderIdsWaitUseAndExpired();
                break;
            }
            default: {
                break;
            }

        }


        return mallOrderIds;

    }

    /**
     * @param mallOrderId
     * @param status
     * @return
     */
    private List<MerchantOrder> getCancelMerchantOrders(Long mallOrderId, OrderStatus status) {

        List<MerchantOrder> canCancelMerchantOrders = new ArrayList<>();
        switch (status) {
            case WAIT_SHOP_PICKUP: {
                canCancelMerchantOrders = merchantOrderRepository.queryAllOrderOfWaitShopPickpup(mallOrderId);
                break;
            }
            case WAIT_SHIPPED: {
                canCancelMerchantOrders = merchantOrderRepository.queryAllOrderOfWaitShipped(mallOrderId);
                break;
            }
            case WAIT_STOCK: {
                canCancelMerchantOrders = merchantOrderRepository.queryAllOrderOfWaitStock(mallOrderId);
                break;
            }
            case WAIT_USE: {
                canCancelMerchantOrders = merchantOrderRepository.queryAllOrderOfWaitUse(mallOrderId);
                break;
            }
            default: {
                break;
            }
        }
        return canCancelMerchantOrders;
    }

//
//    public static void main(String[] args) {
//        BigDecimal discount = BigDecimal.valueOf(48);
//        Double point = new Double(1) / 2;
//        BigDecimal multiplicand = new BigDecimal(point);
//        BigDecimal amount = discount.multiply(multiplicand);
//        System.out.println("+++++++++++++" + multiplicand);
//        System.out.println("+++++++++++++" + amount);
//    }

}
